/*
* Valentine Gladstein
* SOLAR ID 108731583
* Homework #3
* CSE214
* Recitation 03-Phil Ammirato
* Grading TA: Yanjie Li
*/

import java.io.*;
import java.util.*;

public class CSAir { 

   //The CSAir class provides a console-driven menu system for manipulating a CityList object

    private CityList cl; //stores the CityList object associated with this CSAir object

    public CSAir() {
	cl = new CityList();
    }

/**
 * Displays the main menu in the standard output and 
 * gets the user's input selection.
 * @return The user's entry, converted to lowercase.
 */
    public String displayMenu(){

	System.out.println("\n\n-------------------------------");
	System.out.println("Main Menu");
	System.out.println("-------------------------------");
	System.out.println("I: Insert a city into the list");
	System.out.println("A: add a flight path");
	System.out.println("R: Request a flight");
	System.out.println("Q: Quit the pogram");
	System.out.println("-------------------------------\n");
	System.out.println("");
	System.out.print("Please make a selection: ");

	return getInput();

    }

    /**
     * A helper method used internally to read user input from the standard input
     * @return a string containing the user's input
     */
    public String getInput() {
	String s = "";
	InputStreamReader isr = new InputStreamReader(System.in);
	BufferedReader br = new BufferedReader(isr);
	try {
	    s = br.readLine();

	} catch (IOException e) {}
	return s.toLowerCase();
    }
   
  /**
     * Prompts user for input and adds the entered String as a new city
     * into the CityList
     */

    public void insertCity() {
	System.out.println("Enter the name of the city to be added: ");
	try {
	    cl.addCity(getInput());
	    System.out.println("Success!");
	}catch(Exception e){System.out.println(e.getMessage());}
    } 

  /**
     * Prompts user for input and adds a flight between the 
     * entered cities to the CityList's adjacentCities list
     */

    public void addFlight(){
	String origin;
	String destination;

	System.out.println("Enter origin city: ");
	origin = getInput();
	System.out.println("Enter destination city: ");
	destination = getInput();
	try {
	    cl.addPath(origin, destination);
	    System.out.println("Success!");
	}catch(Exception e){System.out.println(e.getMessage());}
    }

 /**
     * Prompts user for input and checks CityList's adjacentCities list
     * to determine whether a flight between the cities is possible.
     */

    public void requestFlight() {
	String origin;
	String destination;

	System.out.println("Enter origin city: ");
	origin = getInput();
	System.out.println("Enter destination city: ");
	destination = getInput();
	try {
	    if (cl.isPath(origin, destination))
		System.out.println("Flight(s) from " + origin + " to " + destination + " are available");
	    else System.out.println("No flight(s) from " + origin + " to " + destination + " are available");
	}catch(Exception e){System.out.println("Invalid entry. Please make a valid entry.");}
    }


    /**
     * Provides a way for the user to quit the program gracefully
     *
     */ 
    public void quit() {
	System.out.println("\nTerminating program normally....\n\n");
	System.exit(0);
    }


    /**
     * Provides a menu-driven system for manipulating the 
     * CSAir class. This method loops back to displaying 
     * the menu after every action the user takes.
     *
     */ 
    public static void main(String[] args) {
	CSAir driver = new CSAir();	
	while (true){
	    String str = driver.displayMenu();
	    if (str.equals("i"))
		driver.insertCity();
	    else if (str.equals("a"))
		driver.addFlight();
	    else if (str.equals("r"))
		driver.requestFlight();
	    else if (str.equals("q"))
		driver.quit();
	    else System.out.println("Input not recognized. Please make a valid selection:");
	}
	
    }

}
